<?php
/**
 *author:韩帅
 *createtime:2017/6/21 14:19
 *description:用户平台流水
 */
namespace App\Http\Controllers\App;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\App\BaseController;
use App\Models\UserServerBalance;

class UserBalanceServerLogController extends  BaseController
{
    /**
     * 会员列表
     */
    public function userList(Request $request)
    {
        $return   = [];//存储获取数据

        $name   = trim($request->input('real_name',''));
        $tel    = trim($request->input('user_mobile',''));

        $obj    = new User();

        //判断是否有筛选条件
        if (!empty($name)) {
            $obj =  $obj->where('real_name','like','%'.$name.'%');
        }
        if (!empty($tel)) {
            $obj = $obj->where('user_mobile',$tel);
        }

        $data   = $obj->with('shop')->paginate($this->pageSize)
                                    ->toArray();

        $return = $data;

        if (empty($return['data'])) {

            return self::json($return,'数据为空',200);
        }

        $return = User::translate($return);

        return self::json($return,'成功',200);
    }

    /**
     * 会员详情
     */
    public function userInfo(Request $request)
    {

        $return = [''];
        $userId = $request->get('userId','');

        if (empty($userId)) {
            return self::json(array(),'ID不能为空',400);
        }

        $data   = User::find($userId);

        $return = $data;
        if (empty($data)) {
            return self::json($return,'数据为空',200);
        }
        return self::json($return,'成功',200);
    }

    /**
     * 会员修改
     */
    public function userUpdate(Request $request)
    {

        $userId        = trim($request->input('id',''));
        $user_nickname = trim($request->input('user_nickname',''));  //昵称
        $into_queque   = $request->input('into_queque',0);          //是否能进队列
        $status        = $request->input('status',0);               //是否能登录
        $user_pwd      = $request->input('user_pwd');              //登录密码更新
        $real_name     = trim($request->input('real_name',''));      //真实姓名
        $id_card       = trim($request->input('id_card',''));        //身份证号

        if (empty($userId) || empty($user_nickname) || empty($real_name) || empty($id_card)) {
            return self::error('必填字段不能为空');
        }

        $obj = User::find($userId);

        if (empty($obj)) {
            return self::error('本条记录不存在');
        }

        $obj->user_nickname = $user_nickname;

        if (!empty($user_pwd)) {

            $user_pwd = User::create_pwd($user_pwd);
        }else {
            $user_pwd = $obj->user_pwd;
        }

        $obj->user_pwd      = $user_pwd;
        $obj->into_queque   = $into_queque;
        $obj->status        = $status;
        $obj->id_card       = $id_card;

        $res = $obj->save();

        if ($res == false) {
            return self::error(array(),'更新失败',400);
        }
        return self::success('成功');
    }

    /**
     * 平台会员流水查看
     */
    public function balanceLog(Request $request)
    {
        $return      = []; //存放取出的数据
        $newArr      = []; //存放处理后的数据
        $user_id     = $request->input('user_id','');
        $order_sn    = $request->input('order_sn','');    //订单号
        $action_type = $request->input('action_type',''); //进出账选择
        $s_time      = $request->input('s_time');//开始时间
        $e_time      = $request->input('e_time');//结束时间

        //必填参数判断
        if (empty($user_id)) {
            return self::error('用户ID不能为空');
        }

        $obj = new UserServerBalance();

        $obj = $obj->where('user_id',$user_id);

        //筛选条件判断
        if (!empty($order_sn)) {

            $obj = $obj->where('order_sn',$order_sn);
        }

        if (!empty($action_type)) {

           $obj = $obj->where('action_type',$action_type);
        }

        if($s_time > 0){

            $obj = $obj->where('created_at','>=',$s_time);
        }

        if($e_time > 0){

            $obj = $obj->where('created_at','<=',$e_time);
        }

        $data   = $obj->orderBy('created_at','desc')->paginate($this->pagesize)->toArray();

        $return = $data;

        if (empty($return['data'])) {
            return self::json($return,'获取数据为空');
        }

        $objB   = new UserServerBalance();
        $newArr = $objB->translate($return);

        return self::json($newArr,'成功');
    }

    /**
     * 平台所有用户流水列表
     */
    public function serverList(Request $request)
    {
        $return      = []; //存放取出的数据
        $newArr      = []; //存放处理后的数据
        $order_sn    = $request->input('order_sn','');    //订单号
        $user_mobile = $request->input('user_mobile',''); //手机号
        $card_name   = $request->input('card_name','');    //用户名
        $action_type = $request->input('action_type',''); //进出账选择
        $s_time      = $request->input('s_time');//开始时间
        $e_time      = $request->input('e_time');//结束时间

        $obj = new UserServerBalance();

        //筛选条件判断
        if (!empty($order_sn)) {

            $obj = $obj->where('order_sn',$order_sn);
        }
        //手机号
        if (!empty($user_mobile)) {

            $obj = $obj->where('user_mobile',$user_mobile);
        }
        //用户名
        if (!empty($card_name)) {

            $obj = $obj->where('card_name',$card_name);
        }

        if (!empty($action_type)) {

            $obj = $obj->where('action_type',$action_type);
        }

        if($s_time > 0){

            $obj = $obj->where('created_at','>=',$s_time);
        }

        if($e_time > 0){

            $obj = $obj->where('created_at','<=',$e_time);
        }

        $data   = $obj->orderBy('created_at','desc')->paginate($this->pagesize)->toArray();

        $return = $data;

        if (empty($return['data'])) {
            return self::json($return,'获取数据为空');
        }

//        $objB   = new UserServerBalance();
//        $newArr = $objB->translate($return);

        return self::json($return,'成功');
    }


}